package com.example.web;

import com.example.business.ArticleService;
import com.example.data.DataUtils;
import com.example.data.model.Article;
import com.example.exception.ValidateException;
import com.example.page.Page;
import com.example.page.Pageable;
import com.example.utils.AuthToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Article 编辑接口 API
 * Created by yangbajing(yangbajing@gmail.com) on 2017-01-12.
 */
@RestController
@RequestMapping("/demo/article")
@Api(value = "demo-article", description = "关于文章的CUD")
public class ArticleController {
    private final ArticleService articleService;

    @Autowired
    public ArticleController(ArticleService articleService) {
        this.articleService = articleService;
    }

    @PutMapping
    @ApiOperation(value = "更新文章", notes = "更新文章")
    public Article updateArticle(@RequestBody @Valid Article article, BindingResult result) {
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }
//        article.setUpdateBy(AuthToken.getFromThreadLocal().get().id);
        article.setUpdateDate(LocalDateTime.now());
        articleService.updateArticle(article);
        return article;
    }

    @DeleteMapping(path = "{id}")
    @ApiOperation(value = "根据id删除文章", notes = "根据id删除文章")
    public ResponseEntity deleteArticle(@PathVariable Integer id) {
        articleService.deleteArticle(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @PostMapping
    @ApiOperation(value = "创建文章", notes = "创建文章")
    public Article createArticle(@RequestBody Article article) {
//        article.setCreateBy(AuthToken.getFromThreadLocal().get().id);
        article.setCreateDate(LocalDateTime.now());
        articleService.createArticle(article);
        return article;
    }

    @GetMapping(path = "{id}")
    @ApiOperation(value = "根据id查找文章", notes = "根据id查找文章")
    public ResponseEntity<Article> findOne(@PathVariable Integer id) {
        return articleService.findById(id)
                .map(Article -> new ResponseEntity<>(Article, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<Article>(HttpStatus.NOT_FOUND));
    }

    @GetMapping(path = "list")
    @ApiOperation(value = "查找所有文章", notes = "查找所有文章")
    public List<Article> findAll() {
        return articleService.findAll();
    }

    @GetMapping(path = "page")
    @ApiOperation(value = "查找所有文章并分页", notes = "查找所有文章并分页")
    public Page<Article> findPage(
            @RequestParam(name = "category", required = false) Integer category,
            @RequestParam(name = "title", required = false) String title,
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "beginDate", required = false) String beginDate,
            @RequestParam(name = "endDate", required = false) String endDate,
            @RequestParam(value = "page", required = false, defaultValue = Pageable.STR_PAGE_INDEX) Integer page,
            @RequestParam(value = "size", required = false, defaultValue = "5") Integer size,
            @RequestParam(value = "orderField", required = false, defaultValue = "CREATE_DATE") String orderField,
            @RequestParam(value = "direction", required = false, defaultValue = "DESC") String direction) {
        Pageable pageable = DataUtils.pageable(page, size, orderField, direction);
        return articleService.findPage(category, title, pageable, status, beginDate, endDate);
    }

}
